﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using Utilitys;

namespace Enterprise.Web
{
    public class TemplatePageHelper {

        public double MathRound(double d, int digits) {
            return Math.Round(d, digits);
        }

        public string ClearHtmlTag(string htmlStr) {
            if (string.IsNullOrEmpty(htmlStr)) return "";
            Regex clearHTMLRex = new Regex(@"<[^>]*>"
                , RegexOptions.Singleline | RegexOptions.IgnoreCase);
            return clearHTMLRex.Replace(htmlStr, "");
        }

        public string HtmlEncode(string htmlStr) {
            return HttpUtility.HtmlEncode(htmlStr);
        }

        public string HtmlDecode(string htmlStr) {
            return HttpUtility.HtmlDecode(htmlStr);
        }


        public string ClearHtmlDecodeAndEncode(string htmlStr) {
            return HtmlEncode(HtmlDecode ( ClearHtmlTag(htmlStr)));
        }

        public string ClearHtmlAndEncode(string htmlStr) {
            return HtmlEncode(ClearHtmlTag(htmlStr));
        }

        public string UrlEncode(string htmlStr) {
            return HttpUtility.UrlEncode(htmlStr);
        }

        public string GetDateStr(DateTime date) {
            return date.Year + "-" + date.Month + "-" + date.Day;
        }

        /// <summary>
        /// 自动完成缺失的HTML标签，以及去除多余的HTML标签
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public string CompHtml(string html) {
            return HtmlHelper.AutoHtmlTagCompletion(html);
        }
    }
}
